package com.hellomeng.io;

import java.io.*;

//文件io节点流之字符流
public class FileReaderWriterTest {
    public static void main(String[] args) throws IOException {
       // readerChar();
        //readerArr();
        writeChar();
    }

    public static void readerArr() throws IOException {
        File file = new File("TestData/readerFile.txt");
        System.out.println(file.getAbsoluteFile());
        FileReader fileReader = new FileReader(file);

        char[] cbuf = new char[10];
        //每次读入10个字符
        int len = fileReader.read(cbuf); //范围读入的个数，-1表示没了
        while (len !=-1){
            for (int i = 0; i <len ; i++) {
                System.out.print(cbuf[i]);
            }
            len = fileReader.read(cbuf);
        }
        fileReader.close();
        /*
        ajajajaj jajaja ajajaj jajaja
        你好，我好
        hello,world
         */
    }

    public static void readerChar() throws IOException {
        File file = new File("TestData/readerFile.txt");
        System.out.println(file.getAbsoluteFile());

        //读入文件流
        FileReader fileReader = new FileReader(file);
        int data = fileReader.read(); //返回读入的一个字符，到文件末尾，返回-1。
        while (data != -1){
            System.out.print((char)data);
            data = fileReader.read();
        }
        fileReader.close();
        /*输出
            ajajajaj jajaja ajajaj jajaja
            你好，我好
            hello,world
        */
    }

    public static void writeChar() throws IOException {
        File file = new File("TestData/writeFile.txt");
        FileWriter fileWriter = new FileWriter(file);

        fileWriter.write("你好，nihao");

        fileWriter.close();
    }
}
